package com.ouc.edu.ssm.main;

import com.ouc.edu.ssm.bean.Chief_info;
import com.ouc.edu.ssm.bean.Central_station;
import com.ouc.edu.ssm.bean.Marine_station;
import com.ouc.edu.ssm.bean.Paraminfo;
import com.ouc.edu.ssm.service.QueryAllChiefService;
import com.ouc.edu.ssm.service.QueryCentralStationByChiefIdService;
import com.ouc.edu.ssm.service.QueryMarineStationByCentralStationIdService;
import com.ouc.edu.ssm.service.QueryParamInfoByMarineIdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*模拟产生数据文件*/
@Component
class Simulation_file {
    @Autowired
    private QueryAllChiefService queryallservice;
    @Autowired
    private QueryCentralStationByChiefIdService queryCentralStationByChiefIdService;
    @Autowired
    private QueryMarineStationByCentralStationIdService queryMarineStationByCentralStationIdService;
    @Autowired
    private QueryParamInfoByMarineIdService queryParamInfoByMarineIdService;

    public void createFile() throws IOException {

        //查询到所有的分局的信息
        List<Chief_info> queryAllChiefServiceList = queryallservice.queryChief();
        //对所有的分局的信息进行遍历
        for (Chief_info chief_info : queryAllChiefServiceList) {
            List<Central_station> central_stations = queryCentralStationByChiefIdService.queryCentralStationByChiefId(chief_info.getId());
            //System.out.println("分局信息："+chief_info.getName()+";"+"中心站："+central_stations);
            for (Central_station central_station : central_stations) {
                List<Marine_station> marine_stations = queryMarineStationByCentralStationIdService.queryMarineStationByCentralStationId(central_station.getId());
                //System.out.println("分局："+chief_info.getName()+" 中心站："+central_station.getName()+" 海洋站名字："+marine_stations);
                for (Marine_station marine_station : marine_stations) {
                    List<Paraminfo> paraminfoList = queryParamInfoByMarineIdService.queryParamInfoByMarineId(marine_station.getId());
                    //循环遍历将每一个海洋站底下的要素对应的paramcode放到一个list中，遍历list，获取文件目录
                    List<String> paramcodeList = new ArrayList();
                    for (Paraminfo paraminfo : paraminfoList) {
                        paramcodeList.add(paraminfo.getParamcode());
                    }
                    for (int i = 0; i < paramcodeList.size(); i++) {
                        //获取到目前的时间
                        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd");
                        SimpleDateFormat sdf1 = new SimpleDateFormat("YYYYMMddHHmm");
                        Date date = new Date();
                        String currentTime = sdf.format(date);
                        String currentTime1 = sdf1.format(date);
                        String rootPath = "E:" + File.separator + "test" + File.separator + chief_info.getName()+File.separator+central_station.getName()+File.separator+marine_station.getName()+File.separator+currentTime1;
                        String path = rootPath +  File.separator +currentTime+ "." + paramcodeList.get(i);
                        File file = new File(rootPath);
                        File file1 = new File(path);
                        if (!file.exists()) {
                            file.mkdirs();
                        }
                        if (!file1.exists()) {
                            try {
                                file1.createNewFile();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        BufferedWriter bw = new BufferedWriter(new FileWriter(file1));
                        bw.write("DT" + currentTime1);
                        bw.flush();
                        bw.close();
                    }
                }
            }
        }


    }
}
